#!/usr/bin/env python
# encoding: utf-8
"""
test_atom_compact.py

Created by Dag Brattli on 2008-03-02.
Copyright (c) 2009 Dag Brattli. All rights reserved.
"""

import sys
import os
import logging

logging.basicConfig(level=logging.WARNING)

from elements import atom_compact as atom

def test_atom_compact(filename="tests/atom.xml"):
    data = open(filename).read()
    d = atom.Feed()
    d.from_string(data)
    
    # Render as compact dict
    x = d.to_dict(compact=True)
    assert(x['feed']['title'] == "Example Feed")
    assert(x['feed']['entry'][0]['summary'] == "Some text.")
    assert(x['feed']['entry'][0]['link'][0]['href'] == "http://example.org/2003/12/13/atom03")
    assert(x['feed']['entry'][0]['app$control']['app$draft'] == "Testing Draft")
    
    # Render as verbose dict
    y = d.to_dict(compact=False)
    assert(y['feed']['title']['$t'] == "Example Feed")
    assert(y['feed']['entry'][0]['summary']['$t'] == "Some text.")
    assert(y['feed']['entry'][0]['link'][0]['href'] == "http://example.org/2003/12/13/atom03")
    assert(y['feed']['entry'][0]['app$control']['app$draft']['$t'] == "Testing Draft")

    # Parse compact dict
    d = atom.Feed()
    d.from_dict(x)    
##    print d.to_string()
    assert(d.title.text == "Example Feed")
    assert(d.entry[0].summary.text == "Some text.")
    assert(d.entry[0].link[0].href == "http://example.org/2003/12/13/atom03")
    assert(d.entry[0].control.draft == "Testing Draft")

    # Parse verbose dict
    d = atom.Feed()
    d.from_dict(y)
##    print d.to_string()
    assert(d.title.text == "Example Feed")
    assert(d.entry[0].summary.text == "Some text.")
    assert(d.entry[0].link[0].href == "http://example.org/2003/12/13/atom03")
    assert(d.entry[0].control.draft == "Testing Draft")

if __name__ == '__main__':
	test_atom_compact("atom.xml")

